import { requestWOMeta, UserRoleEntityDTO } from '../data/user.roles.dto.ts';
import { RoleEntity } from '../models/role.entity.ts';
import { v4 as uuidv4 } from 'uuid';
import { UserRoleDTO } from '../data/user.dto.ts';

export function roleListMapper(data: UserRoleEntityDTO[]): RoleEntity[] {
  return data.map((item) => {
    const role = new RoleEntity();
    role._id = item._id;
    role.name = item.name;
    role.description = item.description || '';
    role.isActive = item.isActive;
    role.type = item.type;
    role.permissions_num = item.permissions;
    role.createdAt = item.createdAt;
    role.updatedAt = item.updatedAt;
    return role;
  });
}

export function roleDetailMapper(data: requestWOMeta): RoleEntity {
  const role = new RoleEntity();
  role._id = uuidv4();
  role.name = data.name;
  role.description = data.description || '';
  role.isActive = true;
  role.type = data.type;
  role.permissions = data.permissions || [];
  role.permissions_num = data.permissions.length;
  role.createdAt = new Date().toString();
  role.updatedAt = new Date().toString();
  return role;
}

export function roleItemMapper(data: UserRoleDTO): RoleEntity {
  const role = new RoleEntity();
  role._id = data._id;
  role.name = data.name;
  role.description = data.description || '';
  role.isActive = data.isActive;
  role.type = data.type;
  role.permissions = data.permissions;
  role.permissions_num = data.permissions.length;
  role.createdAt = data.createdAt;
  role.updatedAt = data.updatedAt;
  return role;
}
